<?php (defined('BASEPATH')) OR exit('No direct script access allowed');

class Auth extends MY_Model {
	
	function get_menu_new(){
		$this->load->database();	

		$id = $this->session->userdata('user_id');
		$menu = array();
		
		$sql = "SELECT sys_groups.* FROM sys_users, sys_groups WHERE sys_users.group_id=sys_groups.group_id AND user_id='$id'";
		$query = $this->db->query($sql);
		
		foreach($query->result() as $row)
		{
			$menu = json_decode($row->menu_json,TRUE);
		}
		
		$this->db->close();
		
		return $menu;
	}
	
	function get_menu(){
		$id = $this->session->userdata('user_id');
		$this->load->database();					
		$sql = "SELECT sys_groups.* FROM sys_users, sys_groups WHERE sys_users.group_id=sys_groups.group_id AND user_id='$id'";
		$query = $this->db->query($sql);
		
		$id_sys_menus = array();
		foreach($query->result() as $row)
		{
			$id_sys_menus = json_decode($row->access,TRUE);
		}
		$in = "(";
		foreach($id_sys_menus as $row){
			$in .= $row.",";
		}
		$in = rtrim($in,",").")";
		$sql = "SELECT * FROM sys_menus WHERE menu_id in $in AND parent='0' ORDER BY `order` ASC;";
		$query = $this->db->query($sql);
		$sys_menus = array();
		foreach($query->result() as $row)
		{
			$sys_menus[$row->menu_id]['name'] = $row->name;
			$sys_menus[$row->menu_id]['alias'] = $row->alias;
			$sys_menus[$row->menu_id]['url'] = $row->link;
			$sys_menus[$row->menu_id]['icon'] = $row->icon;
			$sys_menus[$row->menu_id]['sub'] = $this->_get_child($row->menu_id,$in);
		}
		$this->db->close();
		
		return $sys_menus;
	}
	
	function _get_child($id,$in){
		$this->load->database();					
		$sql = "SELECT * FROM sys_menus WHERE parent='$id' AND menu_id in $in ORDER BY `order` ASC;";
		$query = $this->db->query($sql);		
		$sys_menus = array();
		foreach($query->result() as $row)
		{
			$sys_menus[$row->menu_id]['name'] = $row->name;
			$sys_menus[$row->menu_id]['alias'] = $row->alias;
			$sys_menus[$row->menu_id]['url'] = $row->link;
			$sys_menus[$row->menu_id]['icon'] = $row->icon;
			$sys_menus[$row->menu_id]['sub'] = $this->_get_child($row->menu_id,$in);
		}
		$this->db->close();
		return $sys_menus;
	}
	
	function get_access(){
		$id = $this->session->userdata('user_id');
		$this->load->database();					
		$sql = "SELECT sys_groups.* FROM sys_users, sys_groups WHERE sys_users.group_id=sys_groups.group_id AND user_id='$id';";
		$query = $this->db->query($sql);
		
		$id_sys_menus = array();
		foreach($query->result() as $row)
		{
			$id_sys_menus = json_decode($row->access,TRUE);
		}
		$in = "(";
		foreach($id_sys_menus as $row){
			$in .= $row.",";
		}
		$in = rtrim($in,",").")";
		$sql = "SELECT * FROM sys_menus WHERE menu_id in $in;";
		$query = $this->db->query($sql);
		$access = array();
		foreach($query->result() as $row)
		{
			$access[$row->menu_id] = $row->id_access;
			
		}
		//var_dump($access);
		$this->db->close();
		return $access;
	}
	
	function check_token(){
		$id = $this->session->userdata('user_id');
		$token = $this->session->userdata('token');
		$check = false;
		$this->load->database();					
				$sql = "SELECT * FROM sys_users WHERE user_id='$id' AND token='$token'";
				$query = $this->db->query($sql);
				
				if($query->num_rows() > 0){
					$check = true;
					$sql = "UPDATE sys_users SET is_logged=1 WHERE user_id='$id' AND token='$token'";
					$query = $this->db->query($sql);
				}else{
					$this->session->sess_destroy();
				}
		$this->db->close();
		
		return $check;
	}
	
	function lastLogin(){
		if($this->session->userdata('login') === true){
			$id = $this->session->userdata('user_id');
			$token = $this->session->userdata('token');
			
			$this->load->database();
				$sql = "UPDATE sys_users SET last_login='".date("Y-m-d H:i:s")."' WHERE user_id='$id' AND token='$token'";
				$query = $this->db->query($sql);
			$this->db->close();
		}
	}
	
	function notif($time=''){
		$this->load->database();
			
		$this->db->close();
		
		return $data;
	}
	
	function update_notif(){
		$this->load->database();
			$user_id = $this->session->userdata('user_id');
			
			//update
			$sql= "UPDATE sys_users SET notif='".date("Y-m-d H:i:s")."' WHERE user_id='$user_id'";
			$query=$this->db->query($sql);
		
		$this->db->close();
	}
	
	function update_logged(){
		$this->load->database();
			//update
			$sql= "UPDATE sys_users SET token=0,is_logged=0 WHERE TIME_TO_SEC(TIMEDIFF('".date("Y-m-d H:i:s")."', last_login)) > 1800 AND is_logged=1";
			$query=$this->db->query($sql);
		
		$this->db->close();
	}

}

?>